#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Onewire.h"
#include "Ds1302.h"
#include "stdio.h"
#include "LED.h"
unsigned long ulms,ucLed_Time;
unsigned char pucSeg_Buf[10],pucSeg_Code[8],ucSeg_Pos;
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned char ucState;
unsigned int uiTemp,ucTemp=23;
unsigned char pucRtc[3]={23,59,40};
unsigned char Conr_Flag;
unsigned char ucLed_Dly;
unsigned char ucSeg_flag;
unsigned char ucLed;
unsigned char ucLed_FDly,ucLed_Flag;
void Key_Proc(void);
void Seg_Proc(void);
void Led_Proc(void);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Set_RTC(pucRtc);
	while(1){
	Key_Proc();
	Seg_Proc();
	Led_Proc();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	
	 if(++ucLed_Dly==100){
		 ucLed_Dly=0;
		 ucLed_FDly^=1;
		  Led_Disp(ucLed) ;
	 }		 
	 if(++ucKey_Dly==20) ucKey_Dly=0;
	 if(++ucSeg_Dly==500) ucSeg_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
} 
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down){
		 case 12:
			 if(++ucState==3) ucState=0;
			 break;
		 case 13:
			 Conr_Flag^=1;
		     P0 = 0; 
			 P2 = P2 & 0x1F | 0xA0; // P27～P25 清零，再定位 Y5C 
			 P2 &= 0x1F; // P27～P25 清零
			 break;
		 case 16:
			 if(ucState==2){
				 if(++ucTemp==100){
					 ucTemp=0;
				 }
			 }
			 break;
		 case 17:
			 if(ucState==2){
				 if(--ucTemp==9){
					 ucTemp=99;
				 }
			 }
			 else if(ucState==1){
				 ucSeg_flag^=1;
			 }
			 break;
	 }
	if(ucKey_Up==17){
		if(ucState==1){
			ucSeg_flag^=1;
		}
	}
}
void Seg_Proc(void){
	
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	Read_RTC(pucRtc) ;
	uiTemp=rd_temperature();
	switch(ucState){
		case 0:
			sprintf(pucSeg_Buf,"U1   %3.1f",uiTemp/16.0);
		break;
		case 1:
			if(ucSeg_flag){
				sprintf(pucSeg_Buf,"U2 %02d-%02d",(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			}
			else{
				sprintf(pucSeg_Buf,"U2 %02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1]);
			}
			
			break;
		case 2:
			sprintf(pucSeg_Buf,"U3    %02d",ucTemp);
			break;
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Led_Proc(void){
	if(ucLed_Dly) return;
	ucLed_Dly=1;
	if(!Conr_Flag){
		if((uiTemp/16.0)>(ucTemp/1.0)){
			ucLed_Flag=1;
			// P0 |= 16; //这样给值蜂鸣器会响
			 P0 = 16;
			 P2 = P2 & 0x1F | 0xA0; // P27～P25 清零，再定位 Y5C 
			 P2 &= 0x1F; // P27～P25 清零
		}
		else{
			ucLed_Flag=0;
			 P0 = 0; 
			 P2 = P2 & 0x1F | 0xA0; // P27～P25 清零，再定位 Y5C 
			 P2 &= 0x1F; // P27～P25 清零
		}
	}
	else {
		if(pucRtc[1]==0&&pucRtc[2]==0){
			ucLed_Flag=1;
			ucLed_Time=ulms;
			ucLed|=1;
			 P0 = 16;
			 P2 = P2 & 0x1F | 0xA0; // P27～P25 清零，再定位 Y5C 
			 P2 &= 0x1F; // P27～P25 清零
		}
			else if(ulms-ucLed_Time>=5000){
				ucLed_Flag=0;
				ucLed&=~1;
				P0 = 0; 
			 P2 = P2 & 0x1F | 0xA0; // P27～P25 清零，再定位 Y5C 
			 P2 &= 0x1F; // P27～P25 清零
		}
	}
		if(!Conr_Flag){
		ucLed|=2;
	}
	else{
		ucLed&=~2;
	}
	if(ucLed_Flag){
		if(ucLed_FDly){
			ucLed|=4;
		}
		else{
			ucLed&=~4;
		}
	}
	else{
			ucLed&=~4;
	}
}